#include "Camera.h"


namespace Crows3D
{
   Camera::Camera(void)
   {
      position     = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
      yawPitchRoll = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
   }

   Camera::~Camera(void)
   {
   }

   void Camera::SetPosition(float x, float y, float z)
   {
      position = D3DXVECTOR3(x, y, z);
   }


   void Camera::SetYawPitchRoll(float yaw, float pitch, float roll)
   {
      yawPitchRoll = D3DXVECTOR3(yaw, pitch, roll);
   }


   void Camera::SetupPerspective(float fov, float aspect, float nearClip, float farClip)
   {
      D3DXMatrixPerspectiveFovLH(&proj, fov, aspect, nearClip, farClip);
   }


   void Camera::SetupOrthographic(float width, float height, float nearClip, float farClip)
   {
      D3DXMatrixOrthoLH(&proj, width, height, nearClip, farClip);
   }


   void Camera::GetMatrices(D3DXMATRIX& viewMatrix, D3DXMATRIX& projMatrix)
   {
      D3DXVECTOR3 up     = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
      D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f, 0.0f, -1.0f);
      D3DXMATRIX rotationMatrix;

      D3DXMatrixRotationYawPitchRoll(&rotationMatrix, yawPitchRoll.x, yawPitchRoll.y, yawPitchRoll.z);

      // calc transformed lookAt and up vectors
      D3DXVec3TransformCoord(&lookAt, &lookAt, &rotationMatrix);
	   D3DXVec3TransformCoord(&up,     &up,     &rotationMatrix);

      lookAt = position + lookAt;

      D3DXMatrixLookAtLH(&view, &position, &lookAt, &up);

      viewMatrix = view;
      projMatrix = proj;
   }
}